#ifndef DEMO_SYS_CLOCK_H
#define DEMO_SYS_CLOCK_H

#include "stm32f4xx_hal.h"

void DWT_Init(void);

#define BENCH_TIMES(func, rc, ok, times, header)                                                     \
{                                                                                                    \
    uint32_t start, end, elapsed_cycles;                                                             \
    start = DWT->CYCCNT;                                                                             \
    for (int i = 0; i < times; i++) {                                                                \
        rc = func;                                                                                   \
        if (rc != ok) {                                                                              \
            printf("Error: %s, ret = %08x\n", #func, rc);                                            \
            break;                                                                                   \
        }                                                                                            \
    }                                                                                                \
    end = DWT->CYCCNT;                                                                               \
    elapsed_cycles = (end - start);                                                                  \
    double elapsedTime_s = (double)elapsed_cycles / (double)SystemCoreClock;                         \
    double ops = (times > 0 && elapsed_cycles > 0) ? ((double)times / elapsedTime_s) : 0;            \
    printf("%-25s, %15.6f, %20d, %20.2f\r\n", header, elapsedTime_s, times, ops);                    \
}

#define BENCH_TIMES_VOID(func, times, header)                                                     \
{                                                                                                    \
    uint32_t start, end, elapsed_cycles;                                                             \
    start = DWT->CYCCNT;                                                                             \
    for (int i = 0; i < times; i++) {                                                                \
        func;                                                                                        \
    }                                                                                                \
    end = DWT->CYCCNT;                                                                               \
    elapsed_cycles = (end - start);                                                                  \
    double elapsedTime_s = (double)elapsed_cycles / (double)SystemCoreClock;                         \
    double ops = (times > 0 && elapsed_cycles > 0) ? ((double)times / elapsedTime_s) : 0;            \
    printf("%-25s, %15.6f, %20d, %20.2f\r\n", header, elapsedTime_s, times, ops);                    \
}

#endif //DEMO_SYS_CLOCK_H
